package com.collect.zambia.zhiScene.zhiWidget.zhiDialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.collect.zambia.R;

import java.util.Objects;

public class ZHIDialogAvatar extends Dialog {

    private TextView negtiveBtn;
    private ImageView avatarImageView;

    private final Context context;
    private String mImgUrl;

    public static void showDialog(Context context, String avatarUrl) {
        ZHIDialogAvatar dialogAvatar = new ZHIDialogAvatar(context);
        dialogAvatar.mImgUrl = avatarUrl;
        dialogAvatar.show();
    }

    public ZHIDialogAvatar(Context context) {
        super(context, R.style.CustomDialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_avatar);
        setCanceledOnTouchOutside(true);
        setCancelable(true);
        initView();
        initViewEvent();
    }

    @Override
    public void show() {
        super.show();
        try {
            WindowManager.LayoutParams lp = Objects.requireNonNull(getWindow()).getAttributes();
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT; //设置宽度
            lp.width = WindowManager.LayoutParams.MATCH_PARENT; //设置宽度
            lp.gravity = Gravity.BOTTOM;
            getWindow().setAttributes(lp);
            refreshViewData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        negtiveBtn = findViewById(R.id.btn_negtive);
        avatarImageView = findViewById(R.id.iv_avatar);
    }

    private void refreshViewData() {
        try {
            if (!TextUtils.isEmpty(mImgUrl) && mImgUrl.startsWith("http")) {
                Glide.with(context).load(mImgUrl).into(avatarImageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initViewEvent() {
        negtiveBtn.setOnClickListener(v -> dismiss());
    }
}
